<?php

namespace App\Http\Controllers\Wechat\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\CartRepo;
use App\Http\Requests\Api\CartRequest;

class CartController extends Controller
{
    public function index(CartRequest $request)
    {
    
    	$rs['status'] = false;
    	$where['user_id'] = $request->user_id;
    	$list = CartRepo::getPages($where,$num=10);
    	if($list) {
    		$rs['status'] = true;
    		$rs['data'] = $list;
    		return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);;
    	}
    	return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);;
    }

    public function create(CartRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        
        $flag = CartRepo::create($data);
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        return response()->json($rs);
    }   

    public function decrease(CartRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        
        $flag = CartRepo::decrease($data);
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    public function delete(CartRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        //dd($data);
        $flag = CartRepo::delete($data);
        if($flag) {
            $rs['status'] = true;
            $rs['data'] = $data;
            return response()->json($rs);
        }
        return response()->json($rs);
    }
}
